package com.mygdx.game.desktop.demo;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.PolygonRegion;
import com.badlogic.gdx.graphics.g2d.PolygonSprite;
import com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.EarClippingTriangulator;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.ShortArray;

public class ColdDownTestGame extends Game {

	private Stage stage;
	private Texture ground;
	private Texture hand;
	private Texture outerRing;
	private ColdDownIcon icon;

	@Override
	public void create() {
		Gdx.app.setLogLevel(Application.LOG_DEBUG);
		stage = new Stage();
		//Gdx.input.setInputProcessor();
		ground = new Texture(Gdx.files.internal("images/skill/frostbolt.jpg"));
		hand = new Texture(Gdx.files.internal("images/skill/frostbolt.jpg"));
		outerRing = new Texture(Gdx.files.internal("images/skill/frostbolt.jpg"));

		icon = new ColdDownIcon(new TextureRegion(ground), new TextureRegion(outerRing), new PolygonSpriteBatch(),
				new Image(hand), 3.0f);
		stage.addActor(icon);
	}

	public void render() {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

	}

	public void dispose() {

	}

	// 当前切割位置的枚举
	public enum IntersectAt {
		NONE, TOP, BOTTOM, LEFT, RIGHT;
	}

	/**
	 * 冷却图标类
	 * 
	 * @author Administrator
	 *
	 */
	class ColdDownIcon extends Image {
		private TextureRegion texture;// 裁剪画的纹理
		private TextureRegion ground;// 背景纹理，裁剪镂空后要露出来的，也就是原始技能图标
		private TextureRegion outerRing;// 图标外圈
		private Image handEffect;// 指针效果，本来打算使用粒子效果的

		private PolygonSpriteBatch polyBatch;// 画多边形的

		private Vector2 center;
		private Vector2 centerTop;// 从上面中间开始
		private Vector2 leftTop;
		private Vector2 leftBottom;
		private Vector2 rightBottom;
		private Vector2 rightTop;
		private Vector2 progressPoint;
		private float[] fv;// 裁剪画图使用的点阵{point1.x,point1.y,point2.x,point2.y ......}
		private Vector2 intersectPoint;// 当前切割在边上的点

		// 当前正在切割的位置
		private IntersectAt intersectAt;
		private float liveTime;// 本次cd已执行时间
		private float coldDownTime;// cd一次所需时间
		private boolean startColdDown;

		public ColdDownIcon(TextureRegion ground, TextureRegion outerRing, PolygonSpriteBatch polyBatch,
				Image handEffect, float coldDownTime) {
			super(ground);
			this.ground = ground;
			this.texture = ground;
			this.outerRing = outerRing;
			this.polyBatch = polyBatch;
			this.handEffect = handEffect;
			this.handEffect.setVisible(false);

			handEffect.setOrigin(this.getWidth() / 2, this.getHeight() / 2);
			this.coldDownTime = coldDownTime;
			// 计算各点内部坐标
			center = new Vector2(this.getWidth() / 2, this.getHeight() / 2);
			centerTop = new Vector2(this.getWidth() / 2, this.getHeight());
			leftTop = new Vector2(0, this.getHeight());
			leftBottom = new Vector2(0, 0);
			rightBottom = new Vector2(this.getWidth(), 0);
			rightTop = new Vector2(this.getWidth(), this.getHeight());
			progressPoint = new Vector2(this.getWidth() / 2, this.getHeight() / 2);

			setColor(Color.RED);

			setPercentage(0);
		}

		public void startColdDown() {
			this.startColdDown = true;
			this.liveTime = 0;
			this.setPercentage(0);
			this.handEffect.setVisible(true);
		}

		public void endColdDown() {
			this.startColdDown = false;
			this.liveTime = 0;
			this.setPercentage(0);
			this.handEffect.setVisible(false);
		}

		// 计算切线的最远点
		private Vector2 IntersectPoint(Vector2 line) {
			Vector2 v = new Vector2();
			boolean isIntersect;

			// check top
			isIntersect = Intersector.intersectSegments(leftTop, rightTop, center, line, v);// 切割线和上边的交点v

			// check bottom
			if (isIntersect) {
				intersectAt = IntersectAt.TOP;
				return v;
			} else
				isIntersect = Intersector.intersectSegments(leftBottom, rightBottom, center, line, v);

			// check left
			if (isIntersect) {
				intersectAt = IntersectAt.BOTTOM;
				return v;
			} else
				isIntersect = Intersector.intersectSegments(leftTop, leftBottom, center, line, v);

			// check bottom
			if (isIntersect) {
				intersectAt = IntersectAt.LEFT;
				return v;
			} else
				isIntersect = Intersector.intersectSegments(rightTop, rightBottom, center, line, v);

			if (isIntersect) {
				intersectAt = IntersectAt.RIGHT;
				return v;
			} else {
				intersectAt = IntersectAt.NONE;
				return null;
			}
		}

		// 设置百分比，顺时针
		private void setPercentage(float percent) {
			// 100 % = 360 degree
			// ==> percent % => (percent * 360 / 100) degree

			float angle = convertToRadians(90); // percent = 0 => angle = -90
			angle -= convertToRadians(percent * 360 / 100);

			float len = this.getWidth() > this.getHeight() ? this.getWidth() : this.getHeight();
			float dy = (float) (Math.sin(angle) * len);
			float dx = (float) (Math.cos(angle) * len);
			Vector2 line = new Vector2(center.x + dx, center.y + dy);

			intersectPoint = IntersectPoint(line);
			//
			float l = intersectPoint.dst(center.x, center.y);
			float sy = 2 * l / getHeight();

			handEffect.setScaleY(sy);

			if (intersectAt == IntersectAt.TOP) {
				if (intersectPoint.x >= this.getWidth() / 2) //
				{
					//
					fv = new float[] { center.x, center.y, centerTop.x, centerTop.y, leftTop.x, leftTop.y, leftBottom.x,
							leftBottom.y, rightBottom.x, rightBottom.y, rightTop.x, rightTop.y, intersectPoint.x,
							intersectPoint.y };
				} else {
					fv = new float[] { // cắt bên trái cạnh
							center.x, center.y, centerTop.x, centerTop.y, intersectPoint.x, intersectPoint.y };

				}
			} else if (intersectAt == IntersectAt.BOTTOM) {
				fv = new float[] { center.x, center.y, centerTop.x, centerTop.y, leftTop.x, leftTop.y, leftBottom.x,
						leftBottom.y, intersectPoint.x, intersectPoint.y };

			} else if (intersectAt == IntersectAt.LEFT) {
				fv = new float[] { center.x, center.y, centerTop.x, centerTop.y, leftTop.x, leftTop.y, intersectPoint.x,
						intersectPoint.y };

			} else if (intersectAt == IntersectAt.RIGHT) {
				fv = new float[] { center.x, center.y, centerTop.x, centerTop.y, leftTop.x, leftTop.y, leftBottom.x,
						leftBottom.y, rightBottom.x, rightBottom.y, intersectPoint.x, intersectPoint.y };
			} else // if (intersectAt == IntersectAt.NONE)
			{
				// 不绘制
				fv = null;
			}
		}

		// 重新绘制函数
		@Override
		public void draw(Batch batch, float parentAlpha) {
//	        super.draw(batch, parentAlpha);
			batch.draw(ground, this.getX(), this.getY());

			if (fv != null && this.startColdDown) {// 画裁剪了的图
				batch.end(); // 注意这里！！！先把原来的停掉
				drawMe();
				batch.begin(); // 注意这里！！再开始！
			}
			if (handEffect.isVisible()) {
				handEffect.setX(this.getX());
				handEffect.setY(this.getY());
				handEffect.draw(batch, parentAlpha);
			}

			batch.draw(outerRing, this.getX(), this.getY());
		}

		@Override
		public void act(float delta) {
			super.act(delta);
			if (this.startColdDown) {// 开始冷却了，计时
				this.liveTime = this.liveTime + delta;
				if (this.liveTime > this.coldDownTime) {// 超出停止
					this.endColdDown();
				} else {
					float percent = this.liveTime * 100 / this.coldDownTime;
					this.setPercentage(percent);
					handEffect.setVisible(true);
					handEffect.setRotation(-percent * 360 / 100);
				}
			}
		}

		// 按点阵列区域绘制图像
		public void drawMe() {
			// 裁剪
			EarClippingTriangulator e = new EarClippingTriangulator();
			ShortArray sv = e.computeTriangles(fv);

			// 创建 polygonRegion.
			PolygonRegion polyReg = new PolygonRegion(texture, fv, sv.toArray());

			// 创建 polySprite.
			PolygonSprite poly = new PolygonSprite(polyReg);

			// (position, origin, rotation, color)
			poly.setOrigin(this.getOriginX(), this.getOriginY());
			poly.setPosition(this.getX(), this.getY());
			poly.setRotation(this.getRotation());
			poly.setColor(this.getColor());

			// 绘制
			polyBatch.begin();
			poly.draw(polyBatch);
			polyBatch.end();
		}

		float convertToDegrees(float angleInRadians) {
			float angleInDegrees = angleInRadians * 57.2957795f;
			return angleInDegrees;
		}

		float convertToRadians(float angleInDegrees) {
			float angleInRadians = angleInDegrees * 0.0174532925f;
			return angleInRadians;
		}

	}

	public static void main(String[] args) {
		new LwjglApplication(new ColdDownTestGame());
	}
}
